package com.ytmz.gateway.security;

import org.springframework.http.HttpCookie;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class AuthHelp {
    static String getTokenString(ServerWebExchange exchange, SecurityProperties securityProperties) {
        MultiValueMap<String, HttpCookie> cookies = exchange.getRequest().getCookies();
        for (String name : cookies.keySet()) {
            if (name.equals("EL-ADMIN-TOEKN")) {
                try {
                    return URLDecoder.decode(cookies.get(name).get(0).getValue(),"UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        return exchange.getRequest().getHeaders().getFirst(securityProperties.getHeader());
    }
}
